package com.lcf.user.delay;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 延时消息实体
 *
 * @author slimina
 */
public class Message implements Delayed {

    private int id;
    private String body;  //消息内容
    private long excuteTime;//执行时间
//    private long interval;//间隔时间
    private int num;//执行次数

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public long getExcuteTime() {
        return excuteTime;
    }

    public void setExcuteTime(long excuteTime) {
        this.excuteTime = excuteTime;
    }

//    public long getInterval() {
//        return interval;
//    }
//
//    public void setInterval(long interval) {
//        this.interval = interval;
//    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public Message(int id, String body, long delayTime,int num) {
        this.id = id;
        this.body = body;
        this.excuteTime = TimeUnit.NANOSECONDS.convert(delayTime*num, TimeUnit.MILLISECONDS) + System.nanoTime();
        this.num = num;
    }

    @Override
    public int compareTo(Delayed delayed) {
        long d = (getDelay(TimeUnit.NANOSECONDS) - delayed.getDelay(TimeUnit.NANOSECONDS));
        return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.excuteTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

}
